package com.blunka.framework.client.ui.dialog;

import java.awt.BorderLayout;
import org.apache.log4j.Logger;

import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;

import com.blunka.framework.client.utils.DesignUtils;

/**
 * 帮助窗体
 * @author Administrator
 *
 */
public class HelpDialog extends JDialog implements HyperlinkListener{

	private static final Logger logger = Logger.getLogger(HelpDialog.class);

	private static HelpDialog dialog = new HelpDialog();

	public static HelpDialog newInstance(){
		return dialog;
	}

	public HelpDialog(){
		this.setTitle("系统建模集成开发工具-使用说明V1.0");
		this.setLayout(new BorderLayout());
		try{

			JEditorPane editorPane = new JEditorPane();
			String path = "file:///"+DesignUtils.getDir()+"/res/help/help.html";
			editorPane.setEditable(false);
			editorPane.addHyperlinkListener(this);
			editorPane.setPage(path);
			JScrollPane scrollPane = new JScrollPane(editorPane);
			add(scrollPane,BorderLayout.CENTER);
		}catch(Exception e){
			logger.error("加载帮助文档失败",e);
		}

		int width = 500;
		int height = getToolkit().getScreenSize().height-50;
		this.setSize(width,height);
		this.setLocation(0,0);
		//this.setModal(true);
		//dialog.setLocationRelativeTo(MainFrame.getMainFrame());
		this.setVisible(true);
	}

	@Override
	public void hyperlinkUpdate(HyperlinkEvent e) {
		if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
			JEditorPane pane = (JEditorPane) e.getSource();
			if (e instanceof HTMLFrameHyperlinkEvent) {
				HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
				HTMLDocument doc = (HTMLDocument) pane.getDocument();
				doc.processHTMLFrameHyperlinkEvent(evt);
			} else {
				try {
					pane.setPage(e.getURL());
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		}
	}
}
